<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class withdraw extends core {
	public $withdraw_mod;
	public function __construct() {
		parent::__construct();
		$this->withdraw_mod = m("withdraw");
	}
	
	public function index() {
		success("ok");
	}

	//申请提现
	public function apply() {
		$withdraw_money = (isset($_POST['withdraw_money']) && floatval($_POST['withdraw_money'])) ? floatval($_POST['withdraw_money']) : 0;
		$bank_name = (isset($_POST['bank_name']) && trim($_POST['bank_name'])) ? trim($_POST['bank_name']) : '';
		$branch_name = (isset($_POST['branch_name']) && trim($_POST['branch_name'])) ? trim($_POST['branch_name']) : '';
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$card_number = (isset($_POST['card_number']) && trim($_POST['card_number'])) ? trim($_POST['card_number']) : '';
		$bank_id = (isset($_POST['bank_id']) && intval($_POST['bank_id'])) ? intval($_POST['bank_id']) : 0; //银行id,大于0覆盖上面4项
		$alipay = (isset($_POST['alipay']) && trim($_POST['alipay'])) ? trim($_POST['alipay']) : '';
		$type = (isset($_POST['type']) && trim($_POST['type'])) ? trim($_POST['type']) : 'commission'; //资金来源(member表的字段)，commission佣金
		
		if (!strlen($type)) error('数据错误');
		//判断表是否存在字段
		$sql = "DESCRIBE {$this->tbp}member {$type}";
		$row = $this->db->get_row($sql);
		if (!$row || (stripos($row->Type,'decimal(')===false && stripos($row->Type,'int(')===false)) error('数据错误');
		
		//判断是否足够资金用来提现
		$sql = "SELECT {$type} FROM {$this->tbp}member WHERE id='{$this->member_id}'";
		$money = floatval($this->db->get_var($sql));
		if ($money<$withdraw_money) error('提现金额错误');
		
		$bank = new stdClass();
		if ($bank_id>0) {
			$sql = "SELECT * FROM {$this->tbp}member_bank WHERE id='{$bank_id}' AND member_id='{$this->member_id}'";
			$bank = $this->db->get_row($sql);
			if ($bank) {
				$bank_name = $bank->bank_name;
				$branch_name = $bank->branch_name;
				$name = $bank->name;
				$card_number = $bank->card_number;
			}
		} else {
			$bank->bank_name = $bank_name;
			$bank->branch_name = $branch_name;
			$bank->name = $name;
			$bank->card_number = $card_number;
		}
		$sql = "INSERT INTO {$this->tbp}withdraw(member_id, withdraw_money, name, bank_card,
			bank_name, bank_branch, add_time, status, type, ip) VALUES(
			'{$this->member_id}', '{$withdraw_money}', '{$name}', '{$card_number}',
			'{$bank_name}', '{$branch_name}', '".time()."', '0', '{$type}', '{$this->ip}')";
		$this->db->query($sql);
		
		$money -= $withdraw_money;
		$sql = "UPDATE {$this->tbp}member SET {$type}='{$money}' WHERE id='{$this->member_id}'";
		$this->db->query($sql);
		$_SESSION["member"]->$type = $money;
		success($bank);
	}

	//20171221 by @jsong 弃用，使用member/withdraw_history代替
	//申请提现历史
	public function history() {
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 15;
		$sql = "SELECT withdraw_money, add_time, withdraw_memo as memo, status FROM {$this->tbp}withdraw
			WHERE member_id='{$this->member_id}' ORDER BY id DESC LIMIT {$offset}, {$pagesize}";
		$results = $this->db->get_results($sql);
		if ($results) {
			foreach ($results as $g) {
				$g->status_name = $this->withdraw_mod->status_name($g->status);
				$g->add_time = date("Y-m-d", $g->add_time);
				$g->title = '申请提现';
			}
		}
		success($results);
	}
	
	//检测提现密码
	public function password(){
		$password = $this->request->post('password');
		if ($password=='') error('提现密码不能为空');
		$sql = "SELECT withdraw_password, withdraw_salt FROM {$this->tbp}member WHERE id='{$this->member_id}'";
		$row = $this->db->get_row($sql);
		$crypt_password = crypt_password($password, $row->withdraw_salt);
		if ($row->withdraw_password!=$crypt_password) error('提现密码不正确');
	    success("ok");
    }

	public function complete(){
	    success("ok");
    }
}
